#include "add_http.h"
#include "ui_add_http.h"

#include <QDebug>

ADD_HTTP::ADD_HTTP(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ADD_HTTP)
{
    ui->setupUi(this);


    connect(ui->icon,SIGNAL(clicked()),this,SLOT(icon_button_slot()));
}

ADD_HTTP::~ADD_HTTP()
{
    delete ui;
}

void ADD_HTTP::set_button(MButton *button)
{
    butt = button;
}

void ADD_HTTP::icon_button_slot()
{
    // 使用QFileDialog打开文件选择对话框
    QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "图片 (*.png *.jpg *.bmp)");

    if (!fileName.isEmpty()) {
        // 更新标签以显示选中的文件路径
        ui->icon->setText("");
        ui->icon->setIconSize(QSize(50,50));
        ui->icon->setIcon(QIcon(fileName));

        if(butt != nullptr)
        {
            butt->set_iconpath(fileName);
        }
    }
}

void ADD_HTTP::accept()
{
    if(ui->name->text() == "")
    {
        QMessageBox::warning(this,tr("提示框"),tr("未添加网页名称"),
                             QMessageBox::Yes,QMessageBox::Yes);
        return;
    }

    if(ui->path->text() == "")
    {
        QMessageBox::warning(this,tr("提示框"),tr("未添加网页网址"),
                             QMessageBox::Yes,QMessageBox::Yes);
        return;
    }

    if(butt != nullptr)
    {
        butt->set_filename(ui->name->text());
        butt->set_filepath(ui->path->text());
        butt->set_isnull(false);
    }

    QDialog::accept();
}

